﻿using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.面料库存.库存记录.service;
using Kingdee.BOS.WebApi.Client;
using mendian;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using static DXApplicationTangche.UC.金蝶.dto.MaterialResultDTO;
using DXApplicationTangche.UC.金蝶.dto;
using static DXApplicationTangche.UC.金蝶.dto.MaterialDTO;

namespace DXApplicationTangche.UC.面料库存.入库.service
{
    public class 入库Service
    {
        public static String post新增面料(DTO面料库存 DTO面料库存)
        {
            String re = RestCall.PostHttp(
                            URL.接口url + "/material/saveMaterial",
                            FunctionHelper.JsonSerialization(
                                DTO面料库存
                                )
                            );
            return re;
        }
        /// <summary>
        /// 供应商一览
        /// </summary>
        /// <returns></returns>
        public static List<KingDeeSupplierDTO> getSupplierAll()
        {
            return
                FunctionHelper.JsonDeserialization<List<KingDeeSupplierDTO>>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/kingdee/getSupplierAll"
                        )
                    );
        }
        /// <summary>
        /// 判断面料是否存在
        /// </summary>
        /// <param name="materialCode"></param>
        /// <returns></returns>
        public static Boolean fabricExisted(String materialCode)
        {
            bool rtn =
                FunctionHelper.JsonDeserialization<Boolean>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/material/fabricExisted?materialCode=" + materialCode
                        )
                    );
            return rtn;
        }
        /// <summary>
        /// 判断面料号输入是否合法，面料号是否重复
        /// </summary>
        public static MaterialMessageResult 面料号合法性判断(String materialNo, String meterialDesc, String composition)
        {
            if (入库Service.fabricExisted(materialNo))
            {
                return new MaterialMessageResult(false, "金蝶里该面料号已存在，请勿重复创建");
            }
            if (String.IsNullOrEmpty(materialNo)
                || String.IsNullOrEmpty(meterialDesc)
                || String.IsNullOrEmpty(composition))
            {
                return new MaterialMessageResult(false, "面料号、描述名称和面料成份不能为空");
            }
            if (materialNo.Contains("/"))
            {
                return new MaterialMessageResult(false, "面料号不能包含特殊字符'/'");
            }
            if (materialNo.Contains("\\"))
            {
                return new MaterialMessageResult(false, "面料号不能包含特殊字符'\\'");
            }
            if (materialNo.Contains("+"))
            {
                return new MaterialMessageResult(false, "面料号不能包含特殊字符'+'");
            }
            if (materialNo.Contains("!"))
            {
                return new MaterialMessageResult(false, "面料号不能包含特殊字符'!'");
            }
            if (materialNo.Contains("("))
            {
                return new MaterialMessageResult(false, "面料号不能包含特殊字符'('");
            }
            if (materialNo.Contains(")"))
            {
                return new MaterialMessageResult(false, "面料号不能包含特殊字符')'");
            }
            if (materialNo.Contains(":"))
            {
                return new MaterialMessageResult(false, "面料号不能包含特殊字符':'");
            }
            if (materialNo.Contains("*"))
            {
                return new MaterialMessageResult(false, "面料号不能包含特殊字符'*'");
            }
            if (materialNo.Contains("["))
            {
                return new MaterialMessageResult(false, "面料号不能包含特殊字符'['");
            }
            if (materialNo.Contains("]"))
            {
                return new MaterialMessageResult(false, "面料号不能包含特殊字符']'");
            }
            if (materialNo.Contains("^"))
            {
                return new MaterialMessageResult(false, "面料号不能包含特殊字符'^'");
            }
            if (materialNo.Contains("&"))
            {
                return new MaterialMessageResult(false, "面料号不能包含特殊字符'&'");
            }
            if (materialNo.Contains("?"))
            {
                return new MaterialMessageResult(false, "面料号不能包含特殊字符'?'");
            }
            if (materialNo.Contains("{"))
            {
                return new MaterialMessageResult(false, "面料号不能包含特殊字符'{'");
            }
            if (materialNo.Contains("}"))
            {
                return new MaterialMessageResult(false, "面料号不能包含特殊字符'}'");
            }
            if (materialNo.Contains("|"))
            {
                return new MaterialMessageResult(false, "面料号不能包含特殊字符'|'");
            }
            if (materialNo.Contains("~"))
            {
                return new MaterialMessageResult(false, "面料号不能包含特殊字符'~'");
            }
            if (materialNo.Contains("#"))
            {
                return new MaterialMessageResult(false, "面料号不能包含特殊字符'#'");
            }
            if (materialNo.Contains(" "))
            {
                return new MaterialMessageResult(false, "面料号不能包含空格");
            }
            return new MaterialMessageResult(true, "");
        }

        //public static KindDeeApplyReturnDTO 金蝶物料保存(String fnumber, DTO面料库存 DTO面料库存)
        //{
        //    // 使用webapi引用组件Kingdee.BOS.WebApi.Client.dll
        //    K3CloudApiClient client = new K3CloudApiClient("http://192.168.3.251/k3cloud/");
        //    var loginResult = client.ValidateLogin("6128407938e2f2", "Administrator", "Sshirt@1234", 2052);
        //    var resultType = JObject.Parse(loginResult)["LoginResultType"].Value<int>();
        //    //登录结果类型等于1，代表登录成功
        //    if (resultType == 1)
        //    {
        //        #region 金蝶接口
        //        //保存
        //        String result = client.Save("BD_MATERIAL", FunctionHelper.JsonSerialization(new Root(DTO面料库存.materialNo, DTO面料库存.meterialDesc, fnumber, 0)));
        //        KingDeeAddResultRoot kingDeeAddResultRoot = FunctionHelper.JsonDeserialization<KingDeeAddResultRoot>(result);
        //        //根据保存返回值取fId Fnumber
        //        //判断返回值，成功和失败分开
        //        //result保存成功
        //        if (Convert.ToBoolean(kingDeeAddResultRoot.Result.ResponseStatus.IsSuccess))
        //        {
        //            //审核提交
        //            String Submitresult = client.Submit("BD_MATERIAL", FunctionHelper.JsonSerialization(new MaterialSubmitDTO(kingDeeAddResultRoot.Result.Number, kingDeeAddResultRoot.Result.Id.ToString())));
        //            String Verifyresult = client.Audit("BD_MATERIAL", FunctionHelper.JsonSerialization(new MaterialVerifyDTO(kingDeeAddResultRoot.Result.Number, kingDeeAddResultRoot.Result.Id.ToString())));
        //            //调用保存面料接口
        //            RestCall.PostHttp(URL.接口url + "/material/saveMaterialWithKingDeeInfo?fId=" +
        //                kingDeeAddResultRoot.Result.Id.ToString() + "&fNumber=" + kingDeeAddResultRoot.Result.Number,
        //                FunctionHelper.JsonSerialization(DTO面料库存)
        //                );
        //            return kingDeeAddResultRoot.build();
        //        }
        //        //result保存失败
        //        else
        //        {
        //            return new KindDeeApplyReturnDTO(false, "金蝶保存失败，请联系李金键");
        //        }
        //        #endregion               
        //    }
        //    //登录失败
        //    else
        //    {
        //        return new KindDeeApplyReturnDTO(false, "登录失败");
        //    }
        //}

    }
    public class MaterialMessageResult
    {
        public Boolean isLeagel { get; set; }
        public String errorMessage { get; set; }
        public MaterialMessageResult(bool isLeagel, string errorMessage)
        {
            this.isLeagel = isLeagel;
            this.errorMessage = errorMessage;
        }
    }
    public class KingDeeSupplierDTO
    {
        public int fsupplierid { get; set; }
        public String fnumber { get; set; }
        public String fname { get; set; }
        public String fshortname { get; set; }
        public KingDeeSupplierDTO()
        {

        }
    }
}
